package JavaBean;

import java.io.UnsupportedEncodingException;
import java.util.Random;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AppMain {

    //日志
    private final static Logger logger = LoggerFactory.getLogger(AppMain.class);
    //随机数
    private static Random rand = new Random();
    
    public static void main(String[] args) {
        
        //2000ms 输出一条数据
        int a1 = 200;
        //共模拟10000条
        int a2 = 1000;
        
        getLog(a1,a2);

    }

    private static void getLog(int a1, int a2) {
        
        for (int i = 0 ; i < a2 ; i ++){

            //a1 和 a2 故bound为2
            int flag = rand.nextInt(2);
            
            switch (flag){
                case 0 :
                    AppStart start = getStart();
                    String jsonString = JSON.toJSONString(start);

                    //打印
                    logger.info(jsonString);
                    break;

                case 1 :
                    JSONObject jo = new JSONObject();
//                    jo.put("",getStart());

                    JSONArray array = new JSONArray();

                    if (rand.nextBoolean()){
                        array.add(getStart());
                        jo.put("data",array);
                    }

                    //时间
                    long millis = System.currentTimeMillis();

                    //打印
                    logger.info(millis + " | " + jo.toJSONString());
                    break;

            }

            try {
                Thread.sleep(a1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        
    }

    //启动日志
    private static AppStart getStart() {

        AppStart app = new AppStart();

        //日志类型
        app.setEn("start");

        //语言
        int flag = rand.nextInt(5);
        switch (flag){
            case 0:
                app.setLanguage("英语");
                break;
            case 1:
                app.setLanguage("中文");
                break;
            case 2:
                app.setLanguage("俄语");
                break;
            case 3:
                app.setLanguage("西班牙语");
                break;
            case 4:
                app.setLanguage("葡萄牙语");
                break;
        }

        long millis = System.currentTimeMillis();
        app.setLt("" + (millis - rand.nextInt(999999999)));

        //入口
        flag = rand.nextInt(5) + 1;
        app.setEnt(flag + "");

        // gmail
        app.setEmail(getEmail(8) + "@163.com");


        //状态 1 2
        flag = rand.nextInt(10) > 8 ? 2: 1;
        app.setAction(flag + " ");

        // 加载时长
        app.setLt(rand.nextInt(20) + "");

        //用户id
        int flag1 = rand.nextInt(3);
        switch (flag1) {
            case 0:
                app.setUname("1");
                break;
            case 1:
                app.setUname("2");
                break;
            case 2:
                app.setUname("3");
                break;
        }

        //用户昵称
        int flag2 = rand.nextInt(3);
        switch (flag2) {
            case 0:
                app.setUname("张三");
                break;
            case 1:
                app.setUname("李四");
                break;
            case 2:
                app.setUname("王五");
                break;
        }

        //用户权限
        int flag3 = rand.nextInt(2);
        switch (flag3) {
            case 0:
                app.setType("root");
                break;
            case 1:
                app.setType("all");
                break;
        }

        return app;
    }

    private static String getEmail(Integer length) {

        StringBuilder str = new StringBuilder();
        Random rand = new Random();

        for (int i = 0; i < length; i++) {
            boolean b = rand.nextBoolean();

            //纯数字
            str.append(String.valueOf(rand.nextInt(10)));

            //字符串
//            if (b) { // 字符串
//                str.append((char) (65 + random.nextInt(26)));// 取得大写字母
//            } else { // 数字
//                str.append(String.valueOf(random.nextInt(10)));
//            }
        }

        return str.toString();
    }

//    错误日志数据
    private static JSONObject getError(){
        AppErrorLog app = new AppErrorLog();

        String[] eb = {"错误摘要：at cn.lift.dfdf.web.AbstractBaseController.validInbound(AbstractBaseController.java:72)",
                "at cn.lift.appIn.control.CommandUtil.getInfo(CommandUtil.java:67)"};
        String[] ed = {"错误详情：java.lang.NullPointerException\\n    " +
                "at cn.lift.appIn.web.AbstractBaseController.validInbound(AbstractBaseController.java:72)\\n " +
                "at cn.lift.dfdf.web.AbstractBaseController.validInbound", "at cn.lift.dfdfdf.control.CommandUtil.getInfo(CommandUtil.java:67)\\n " +
                "at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\\n" +
                " at java.lang.reflect.Method.invoke(Method.java:606)\\n"};

        //错误摘要
        app.setEb(eb[rand.nextInt(eb.length)]);
        //错误详情
        app.setEd(ed[rand.nextInt(ed.length)]);
        JSONObject jsonObject = (JSONObject) JSON.toJSON(app);
        return getCommon("error", jsonObject);
    }

    //公共字段拼接
    private static JSONObject getCommon(String name, JSONObject obj) {
        JSONObject jo = new JSONObject();
        jo.put("ett", (System.currentTimeMillis() - rand.nextInt(99999999)) + "");
        jo.put("en", name);
        jo.put("kv", obj);
        return jo;
    }

    private static char getRandomChar() {
        String s = "";
        int hight;
        int low;
        Random random = new Random();

//        随机数生成
        hight = (176 + Math.abs(random.nextInt(39)));
        low = (161 + Math.abs(random.nextInt(93)));
        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(hight)).byteValue();
        b[1] = (Integer.valueOf(low)).byteValue();

        try {
            s = new String(b, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            System.out.println("错误");
        }
        return s.charAt(0);
    }

    private static String getCONTENT() {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < rand.nextInt(100); i++) {
            str.append(getRandomChar());
        }
        return str.toString();
    }


    private static JSONObject getComment(){
        AppComment com = new AppComment();

        com.setCid(rand.nextInt(10));
        com.setContent(getCONTENT());
        com.setCreateTime((System.currentTimeMillis() - rand.nextInt(99999999)) + "");
        com.setPc(rand.nextInt(1000));
        com.setRc(rand.nextInt(10));

        JSONObject jo = (JSONObject)JSON.toJSON(com);

        return Joint("comment", jo);

    }

    private static JSONObject Joint(String eventName, JSONObject jsonObject) {
        JSONObject jo = new JSONObject();

        jo.put("ett", (System.currentTimeMillis() - rand.nextInt(99999999)) + "");
        jo.put("en", eventName);
        jo.put("kv", jsonObject);
        return jo;
    }






}
